package com.easemob.server.example.api.impl;

import com.easemob.server.example.api.EasemobRestAPI;
import com.easemob.server.example.api.IMUserAPI;
import com.easemob.server.example.comm.body.UserNamesBody;
import com.easemob.server.example.comm.constant.HTTPMethod;
import com.easemob.server.example.comm.helper.HeaderHelper;
import com.easemob.server.example.comm.wrapper.BodyWrapper;
import com.easemob.server.example.comm.wrapper.HeaderWrapper;
import com.easemob.server.example.comm.wrapper.QueryWrapper;
import com.easemob.server.example.comm.wrapper.ResponseWrapper;

public class EasemobIMUsers extends EasemobRestAPI implements IMUserAPI {

	private static final String ROOT_URI = "/users";

	public ResponseWrapper createNewIMUserSingle(Object payload) {
		String url = getContext().getSeriveURL() + getResourceRootURI();
		BodyWrapper body = (BodyWrapper) payload;
		HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();
		
		return getInvoker().sendRequest(HTTPMethod.METHOD_POST, url, header, body, null);
	}

	public ResponseWrapper createNewIMUserBatch(Object payload) {
		String url = getContext().getSeriveURL() + getResourceRootURI();
		BodyWrapper body = (BodyWrapper) payload;
		HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();
		
		return getInvoker().sendRequest(HTTPMethod.METHOD_POST, url, header, body, null);
	}

	public ResponseWrapper getIMUsersByUserName(String userName) {
		String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName;
		HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();
		BodyWrapper body = new UserNamesBody(new String[]{userName});
		
		return getInvoker().sendRequest(HTTPMethod.METHOD_GET, url, header, body, null);
	}

	public ResponseWrapper getIMUsersBatch(Long limit, String cursor) {
		String url = getContext().getSeriveURL() + getResourceRootURI();
		HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();
		QueryWrapper query = QueryWrapper.newInstance().addLimit(limit).addCursor(cursor);
		
		return getInvoker().sendRequest(HTTPMethod.METHOD_GET, url, header, null, query);
	}

	public ResponseWrapper deleteIMUserByUserName(String userName) {
		String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName;
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();
		BodyWrapper body = new UserNamesBody(new String[]{userName});

		return getInvoker().sendRequest(HTTPMethod.METHOD_DELETE, url, header, body, null);
	}

	public ResponseWrapper deleteIMUserBatch(Long limit) {
		String url = getContext().getSeriveURL() + getResourceRootURI();
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();
        QueryWrapper query = QueryWrapper.newInstance().addLimit(limit);

        return getInvoker().sendRequest(HTTPMethod.METHOD_DELETE, url, header, null, query);
	}

	public ResponseWrapper modifyIMUserPasswordWithAdminToken(String userName, Object payload) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/password";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();
        BodyWrapper body = (BodyWrapper) payload;

        return getInvoker().sendRequest(HTTPMethod.METHOD_PUT, url, header, body, null);
	}

	public ResponseWrapper modifyIMUserNickNameWithAdminToken(String userName, Object payload) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName;
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();
        BodyWrapper body = (BodyWrapper) payload;

        return getInvoker().sendRequest(HTTPMethod.METHOD_PUT, url, header, body, null);
	}

	public ResponseWrapper addFriendSingle(String userName, String friendName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/contacts/users/" + friendName;
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_POST, url, header, null, null);
	}

	public ResponseWrapper deleteFriendSingle(String userName, String friendName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/contacts/users/" + friendName;
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_DELETE, url, header, null, null);
	}

	public ResponseWrapper getFriends(String userName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/contacts/users";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_GET, url, header, null, null);
	}

	public ResponseWrapper getBlackList(String userName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/blocks/users";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_GET, url, header, null, null);
	}

	public ResponseWrapper addToBlackList(String userName, Object payload) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/blocks/users";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();
        BodyWrapper body = (BodyWrapper) payload;

        return getInvoker().sendRequest(HTTPMethod.METHOD_POST, url, header, body, null);
	}

	public ResponseWrapper removeFromBlackList(String userName, String blackListName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/blocks/users/" + blackListName;
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_DELETE, url, header, null, null);
	}

	public ResponseWrapper getIMUserStatus(String userName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/status";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_GET, url, header, null, null);
	}

	public ResponseWrapper getOfflineMsgCount(String userName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/offline_msg_count";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_GET, url, header, null, null);
	}

	public ResponseWrapper getSpecifiedOfflineMsgStatus(String userName, String msgId) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/offline_msg_status/" + msgId;
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_GET, url, header, null, null);
	}

	public ResponseWrapper deactivateIMUser(String userName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/deactivate";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_POST, url, header, null, null);
	}

	public ResponseWrapper activateIMUser(String userName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/activate";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_POST, url, header, null, null);
	}

	public ResponseWrapper disconnectIMUser(String userName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/disconnect";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_GET, url, header, null, null);
	}

	public ResponseWrapper getIMUserAllChatGroups(String userName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/joined_chatgroups";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_GET, url, header, null, null);
	}

	public ResponseWrapper getIMUserAllChatRooms(String userName) {
        String url = getContext().getSeriveURL() + getResourceRootURI() + "/" + userName + "/joined_chatrooms";
        HeaderWrapper header = HeaderHelper.getDefaultHeaderWithToken();

        return getInvoker().sendRequest(HTTPMethod.METHOD_GET, url, header, null, null);
	}

	@Override
	public String getResourceRootURI() {
		return ROOT_URI;
	}
}
